var xslFile;
var xmlFile;

var myXslStylesheet;
var myXmlData;
var myXslProc;

var columnSort;
var dataType;
var sortOrder
var query;
var type = "notHTML";
var replaceDiv = "main";

 
 function init() {   
if(document.implementation.createDocument)
{
    var request = new XMLHttpRequest();
    request.overrideMimeType('text/xml');                    
    request.open("GET", xslFile , false);
    request.send(null);
    myXslStylesheet = request.responseXML;
    request.overrideMimeType('text/xml');      
    request.open("GET", xmlFile, false);
    request.send(null);
    myXmlData = request.responseXML;

	myXslProc = new XSLTProcessor();
    myXslProc.importStylesheet(myXslStylesheet);                    
} else {
	alert('Something is very wrong!');        
}
 }
                    
function doTransformation() {

    if(document.implementation.createDocument) {
        
        myXslProc = new XSLTProcessor();
        myXslProc.importStylesheet(myXslStylesheet);
        if(columnSort!=null) myXslProc.setParameter("","columnSort", columnSort);
        if(dataType!=null) myXslProc.setParameter("","dataType", dataType);
        if(sortOrder!=null) myXslProc.setParameter("","sortOrder", sortOrder);
        if(query!=null) myXslProc.setParameter("","query",query);
        myXslProc.setParameter("","type",type);
        
        
		var fragment = myXslProc.transformToFragment(myXmlData, document);
		        
        var tmpDiv = document.getElementById(replaceDiv);
        
        tmpDiv.parentNode.replaceChild(fragment, tmpDiv);
        
    } else { alert('Something is very wrong!'); }
}

function sortColumn(column)
{
    if(column=='playerNumber' || column=='lineup' || column=='games' || column=='playerNumber' || column=='cards' || column=='goalsScored'
    || column=='points' || column=='gamesPlayed' || column=='wins' || column=='draws' || column=='loses' || column=='goalsScored' || column=='goalsConceded'  ) 
        dataType = 'number';
    else dataType = 'text';
    
    if( columnSort == column ) { 
    	if( sortOrder == 'descending' ) {
    		sortOrder = 'ascending';
     	}
     	else {
     		sortOrder = 'descending';
     	}
     }
     else {
		columnSort = column;
     }
	doTransformation();
}

function queryGo(q) {
    query = q;
    doTransformation();
}

function displaySearchBar()
{
    init();
    type = "newHTML";
    replaceDiv = "wrapper";        
    doTransformation();
    type = "notHTML";
    replaceDiv = "main";    
}
function viewTeam(idTeam)
{
    xslFile = "xslt/team_page.xslt";
    xmlFile = "team_data.xml";
    query = idTeam;
    displaySearchBar();  
}

function viewPlayer(idPlayer)
{
    xslFile = "xslt/player_page.xslt";
    xmlFile = "player_data.xml";
    query = idPlayer;
    displaySearchBar(); 
}

function viewFixture(idFixture)
{
    xslFile = "xslt/fixture_page.xslt";
    xmlFile = "fixture_data.xml";
    query = idFixture;
    displaySearchBar(); 
}
        
init();